package com.zscat.mallplus.ums.service.impl;


import com.zscat.mallplus.config.JedisConfig;
import com.zscat.mallplus.ums.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * redis操作Service的实现类
 * https://github.com/shenzhuan/mallplus on 2018/8/7.
 */
@Service
@Slf4j
public class RedisServiceImpl implements RedisService {
    /**
     * 默认编码
     */
    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private JedisConfig jedisConfig;

    @Override
    public String set(String key, String value) {
        //stringRedisTemplate.opsForValue().set(key, value);
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisConfig.redisPoolFactory().getResource();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value{} error", key, value, e);
            return result;
        } finally {
            jedis.close();
        }
        return result;
    }

    @Override
    public String get(String key) {
//        return stringRedisTemplate.opsForValue().get(key);
        Jedis jedis = null;
        String result = null;
        try {
            jedis = jedisConfig.redisPoolFactory().getResource();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("set key:{}error", key, e);
            return result;
        } finally {
            jedis.close();
        }
        return result;
    }

    @Override
    public Long expire(String key, int expire) {
//        return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisConfig.redisPoolFactory().getResource();
            result = jedis.expire(key, expire);
        } catch (Exception e) {
            log.error("expire key:{} error ", key, e);
            return result;
        } finally {
            jedis.close();
        }
        return result;
    }

    @Override
    public Long remove(String key) {
//        stringRedisTemplate.delete(key);
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = jedisConfig.redisPoolFactory().getResource();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("expire key:{} error ", key, e);
            return result;
        } finally {
            jedis.close();
        }
        return result;
    }

    @Override
    public Long increment(String key, long delta) {
        return stringRedisTemplate.opsForValue().increment(key, delta);
    }

    @Override
    public boolean exists(final String key) {
//        return stringRedisTemplate.execute((RedisCallback<Boolean>) connection -> connection.exists(key.getBytes(DEFAULT_CHARSET)));
        Jedis jedis = null;
        boolean result = false;
        try {
            jedis = jedisConfig.redisPoolFactory().getResource();
            result = jedis.exists(key);
        } catch (Exception e) {
            log.error("expire key:{} error ", key, e);
            return result;
        } finally {
            jedis.close();
        }
        return result;
    }
}
